//
//  DrivingConditionView.m
//  MonitoringNetwork
//
//  Created by admin on 2017/12/15.
//  Copyright © 2017年 wtmcxlm. All rights reserved.
//

#import "DrivingConditionView.h"
#import "OperationOnlineCell.h"
#import "CusTableViewCell.h"
#import "HistogramViewCell.h"
#import "GraphViewCell.h"
#import "OperationPiechartCell.h"
#import "ChartTableModel.h"
#import "DaysOperationPiechartlCell.h"

@interface DrivingConditionView()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *conditionTableView;
@property (nonatomic,strong)NSDictionary *mileage;
@property (nonatomic,strong)NSArray *mileageStatis;
@property (nonatomic,strong)NSArray *countTravelCarByhour;

@property (nonatomic,strong)NSArray *runtimeRankDesc;
@property (nonatomic,strong)NSArray *mileageRankDesc;
@property (nonatomic,strong)NSArray *maxSpeed;
@property (nonatomic,strong)NSArray *maxAverageSpeed;
@property (nonatomic,strong)LoadNetView *loadNetView;
@property (nonatomic,assign)BOOL allowLoad;
@end

@implementation DrivingConditionView
- (LoadNetView *)loadNetView
{
    if (!_loadNetView) {
        _loadNetView = [LoadNetView new];
        [self addSubview:_loadNetView];
        [_loadNetView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.mas_equalTo(self);
        }];
        _loadNetView.hidden = NO;
    }
    return _loadNetView;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpUI];
        self.backgroundColor = [UIColor colorWithRed:1.00f green:1.00f blue:1.00f alpha:0.5f];
        _allowLoad = NO;
        _needRefresh = YES;
    }
    return self;
}
- (void)setUpUI
{
    _conditionTableView = [UITableView new];
    _conditionTableView.dataSource = self;
    _conditionTableView.delegate = self;
    _conditionTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    _conditionTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _conditionTableView.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
    [self addSubview:_conditionTableView];
    [_conditionTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self);
        make.bottom.mas_equalTo(self.mas_bottom).offset(-TabBarHeight-10);
    }];
}
- (void)loadNewData
{
    [self.loadNetView start];
    [[NetWorkTool sharedNetworkTool] requestWithUrlString:kURL_MileageStatisByDay method:@"POST" parameters:_param progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(id  _Nullable responseObject) {
        [self.loadNetView endAnimationWithResult:LoadingViewResultTypeSuccess];
        NSLog(@"%@",responseObject);
        if ([responseObject[@"state"] integerValue] == 0) {
            NSDictionary *dict = responseObject[@"obj"];
            self.mileage = dict[@"mileage"];
            self.mileageStatis = dict[@"mileageStatis"];
            self.countTravelCarByhour = dict[@"countTravelCarByhour"];
            self.maxAverageSpeed = dict[@"maxAverageSpeed"];
            self.maxSpeed = dict[@"maxSpeed"];
            self.mileageRankDesc = dict[@"mileageRankDesc"];
            self.runtimeRankDesc = dict[@"runtimeRankDesc"];
            _allowLoad = YES;
        }
         [_conditionTableView.mj_header endRefreshing];
        [_conditionTableView reloadData];
    } failure:^(NSError * _Nonnull error) {
        [self.loadNetView endAnimationWithResult:LoadingViewResultTypeError];
         [_conditionTableView.mj_header endRefreshing];
        NSLog(@"%@",error);
    }];
    
}
- (void)loadDataWithBackGroundParam:(NSDictionary *)param
{
    if (_needRefresh) {
        [self.loadNetView start];
        [[NetWorkTool sharedNetworkTool] requestWithUrlString:kURL_MileageStatisByDay method:@"POST" parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [self.loadNetView endAnimationWithResult:LoadingViewResultTypeSuccess];
            NSLog(@"%@",responseObject);
            if ([responseObject[@"state"] integerValue] == 0) {
                NSDictionary *dict = responseObject[@"obj"];
                self.mileage = dict[@"mileage"];
                self.mileageStatis = dict[@"mileageStatis"];
                self.countTravelCarByhour = dict[@"countTravelCarByhour"];
                self.maxAverageSpeed = dict[@"maxAverageSpeed"];
                self.maxSpeed = dict[@"maxSpeed"];
                self.mileageRankDesc = dict[@"mileageRankDesc"];
                self.runtimeRankDesc = dict[@"runtimeRankDesc"];
                _allowLoad = YES;
                _needRefresh = NO;
            }
             [_conditionTableView.mj_header endRefreshing];
            [_conditionTableView reloadData];
        } failure:^(NSError * _Nonnull error) {
            [self.loadNetView endAnimationWithResult:LoadingViewResultTypeError];
            [_conditionTableView.mj_header endRefreshing];
            NSLog(@"%@",error);
        }];
    }
    
}
- (void)loadDataWithParam:(NSDictionary *)param
{
    
    if (_needRefresh) {
        [self.loadNetView start];
        [[NetWorkTool sharedNetworkTool] requestWithUrlString:kURL_MileageStatisByDay method:@"POST" parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [self.loadNetView endAnimationWithResult:LoadingViewResultTypeSuccess];
            if ([responseObject[@"state"] integerValue] == 0) {
                NSDictionary *dict = responseObject[@"obj"];
                self.mileage = dict[@"mileage"];
                self.mileageStatis = dict[@"mileageStatis"];
                self.countTravelCarByhour = dict[@"countTravelCarByhour"];
                self.maxAverageSpeed = dict[@"maxAverageSpeed"];
                self.maxSpeed = dict[@"maxSpeed"];
                self.mileageRankDesc = dict[@"mileageRankDesc"];
                self.runtimeRankDesc = dict[@"runtimeRankDesc"];
                _allowLoad = YES;
                _needRefresh = NO;
            }
             [_conditionTableView.mj_header endRefreshing];
            [_conditionTableView reloadData];
        } failure:^(NSError * _Nonnull error) {
            [self.loadNetView endAnimationWithResult:LoadingViewResultTypeError];
            [_conditionTableView.mj_header endRefreshing];
            NSLog(@"%@",error);
        }];
    }
   
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_allowLoad) {
        return 9;
    }else{
        return 0;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
        {
            OperationOnlineCell *mileageCell = [OperationOnlineCell new];
            mileageCell.leftTitle = @"行驶总里程数 (km)";
            mileageCell.rightTitle = @"行驶里程平均数 (km)";
            if (_mileage) {
                mileageCell.leftData = [NSString separatedDigitStringWithStr:[NSString stringWithFormat:@"%.2lf",[_mileage[@"totalMileage"] doubleValue]]];
                mileageCell.rightData =[NSString separatedDigitStringWithStr:[NSString stringWithFormat:@"%.2lf",[_mileage[@"avgMileage"] doubleValue]]];
            }
            return mileageCell;
        }
            break;
        case 1:
        {
            OperationOnlineCell *hourCell = [OperationOnlineCell new];
            hourCell.leftTitle = @"行驶时长程数 (h)";
            hourCell.rightTitle = @"行驶时长平均数 (h)";
            if (_mileage) {
                hourCell.leftData = [NSString separatedDigitStringWithStr:[NSString stringWithFormat:@"%.2lf",[_mileage[@"totalRunTime"] doubleValue]]];
                hourCell.rightData = [NSString separatedDigitStringWithStr:[NSString stringWithFormat:@"%.2lf",[_mileage[@"avgRunTime"] doubleValue]]];
            }
            return hourCell;
        }
            break;
        case 2:
        {
            DaysOperationPiechartlCell *milageDistributionCell = [DaysOperationPiechartlCell new];
            if (self.mileageStatis) {
                [milageDistributionCell laodUIWithDataArr:self.mileageStatis];
            }
            milageDistributionCell.title = @"行驶里程分布";
            return milageDistributionCell;
        }
            break;
        case 3:
        {
            HistogramViewCell *drivingCell = [HistogramViewCell new];
            if (self.mileageStatis) {
                [drivingCell laodUIWithDataArr:self.mileageStatis];
            }
            drivingCell.title = @"行驶里程分布";
            return drivingCell;
        }
            break;
        case 4:
        {
            GraphViewCell *graphCell = [GraphViewCell new];
            graphCell.title = @"行驶车辆数趋势图";
            graphCell.tipTitle = @"行驶车辆数";
            if (self.countTravelCarByhour) {
                [graphCell laodGraphWithDataArr:self.countTravelCarByhour andColor:[UIColor colorWithRed:0.21f green:0.50f blue:0.90f alpha:1.00f]];
            }
            return graphCell;
        }
            break;
        case 5:
        {
            CusTableViewCell *totalMileageCell = [CusTableViewCell new];
            totalMileageCell.title = @"行驶里程排行前5名";
            totalMileageCell.leftString = @"排名";
            totalMileageCell.rightString = @"里程数 (km)";
            totalMileageCell.centerString = @"车牌号";
            if(self.mileageRankDesc){
                NSMutableArray *tempArr = [NSMutableArray array];
                for (int i=0; i<self.mileageRankDesc.count; i++) {
                    NSDictionary *dic = self.mileageRankDesc[i];
                    ChartTableModel *model = [ChartTableModel new];
                    model.leftString = [dic[@"runk"] stringValue];
                    model.centerString = dic[@"plateNum"] ;
                    model.rightString = [NSString stringWithFormat:@"%.2lf",[dic[@"totalMileage"] doubleValue]];
                    [tempArr addObject:model];
                }
                [totalMileageCell laodTableWithDataArr:tempArr.copy];
            }
            return totalMileageCell;
        }
        case 6:
        {
            CusTableViewCell *maxHourCell = [CusTableViewCell new];
            maxHourCell.title = @"行驶时长排行前5名";
            maxHourCell.leftString = @"排名";
            maxHourCell.rightString = @"时长 (h)";
            maxHourCell.centerString = @"车牌号";
            if(self.runtimeRankDesc){
                NSMutableArray *tempArr = [NSMutableArray array];
                for (int i=0; i<self.runtimeRankDesc.count; i++) {
                    NSDictionary *dic = self.runtimeRankDesc[i];
                    ChartTableModel *model = [ChartTableModel new];
                    model.leftString = [dic[@"runk"] stringValue];
                    model.centerString = dic[@"plateNum"];
                    model.rightString = [NSString stringWithFormat:@"%.2lf",[dic[@"totalRunTime"] doubleValue]];
                    [tempArr addObject:model];
                }
                [maxHourCell laodTableWithDataArr:tempArr.copy];
            }
            return maxHourCell;
        }
            break;
        case 7:
            {
                CusTableViewCell *averageSpeedCell = [CusTableViewCell new];
                averageSpeedCell.title = @"平均速度排行前5名";
                averageSpeedCell.leftString = @"排名";
                averageSpeedCell.rightString = @"速度 (km/h)";
                averageSpeedCell.centerString = @"车牌号";
                if(self.maxAverageSpeed){
                    NSMutableArray *tempArr = [NSMutableArray array];
                    for (int i=0; i<self.maxAverageSpeed.count; i++) {
                        NSDictionary *dic = self.maxAverageSpeed[i];
                        ChartTableModel *model = [ChartTableModel new];
                        model.leftString = [NSString stringWithFormat:@"%@",dic[@"runk"]];
                        model.centerString = dic[@"plateNum"];
                        model.rightString = [NSString stringWithFormat:@"%.2lf",[dic[@"averagespeed"] floatValue]];
                        [tempArr addObject:model];
                    }
                    [averageSpeedCell laodTableWithDataArr:tempArr.copy];
                }
                return averageSpeedCell;
            }
        break;
        case 8:
        {
            CusTableViewCell *maxSpeedCell = [CusTableViewCell new];
                maxSpeedCell.title = @"最高速度排行前5名";
                maxSpeedCell.leftString = @"排名";
                maxSpeedCell.rightString = @"速度 (km/h)";
                maxSpeedCell.centerString = @"车牌号";
                if(self.maxSpeed){
                    NSMutableArray *tempArr = [NSMutableArray array];
                    for (int i=0; i<self.maxSpeed.count; i++) {
                        NSDictionary *dic = self.maxSpeed[i];
                        ChartTableModel *model = [ChartTableModel new];
                        model.leftString = [NSString stringWithFormat:@"%@",dic[@"runk"]];
                        model.centerString = dic[@"plateNum"];
                        model.rightString = [NSString stringWithFormat:@"%.2lf",[dic[@"speed"] doubleValue]];
                        [tempArr addObject:model];
                    }
                    [maxSpeedCell laodTableWithDataArr:tempArr.copy];
                }
                return maxSpeedCell;
            return nil;
            }
        break;
        default:
            return nil;
            break;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
            return 109;
            break;
        case 1:
            return 109;
            break;
        case 2:
            return 299;
            break;
        case 3:
            return 261;
            break;
        case 4:
            return 269;
            break;
        case 5:
            return 355;
            break;
        case 6:
            return 355;
            break;
        case 7:
            return 355;
            break;
        case 8:
            return 355;
            break;
        default:
            return 0;
            break;
    }
}


@end
